﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TimKen.App;
using TimKen.BussLogic;
using System.Data;

namespace TimKen.Web.Admin.Industry
{
    public partial class Industry_Product : AdminPage
    {
        private PCategoryManager categoryManager = new PCategoryManager();
        private string toadd = "├";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                IndustryID = int.Parse(Request.QueryString["IndustryID"]);

                BindProductCategory("0");
                BindData();
            }
        }

        public int IndustryID
        {
            get
            {
                if (ViewState["IndustryID"] != null && TypeParse.IsNumeric(ViewState["IndustryID"]))
                    return Convert.ToInt32(ViewState["IndustryID"]);
                else
                    return 0;
            }
            set
            {
                ViewState["IndustryID"] = value;
            }
        }



        /// <summary>
        /// 绑定产品分类分级选择列表
        /// </summary>
        private void BindProductCategory(string parentId)
        {
            DataSet ds = categoryManager.GetList(LanguageWhereStr + " AND ParentID=" + parentId);
            if (ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    this.ddlCategory.Items.Add(new ListItem(toadd + " " + dr["CategoryName"].ToString(), dr["CategoryID"].ToString()));
                    toadd += "─┴";
                    BindProductCategory(dr["CategoryID"].ToString());
                    toadd = toadd.Substring(0, toadd.Length - 2);
                }
            }
        }


        private void BindData()
        {
            string sql = @"select   p.ItemCode,p.ProductID,ip.IndustryID,pd.Name  from IndustryProduct  ip     join Industry  i  on i.IndustryID = ip.IndustryID 
           join  Product p  on p.ProductID = ip.ProductID  join PDescription  pd on pd.ProductID = p.ProductID WHERE  pd.LanguageID= 1 AND  ip.IndustryID={0}";
            sql = string.Format(sql, IndustryID);

            this.grdProductList.DataSource = DataAccess.DbHelperSQL.Query(sql);
            this.grdProductList.DataBind();
        }

        protected void button1_Click(object sender, EventArgs e)
        {
            ProductManager pm = new ProductManager();
            DataSet  ds  =  pm.GetList("CategoryID=" + this.ddlCategory.SelectedItem.Value);

            if (ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    int ProductID = (int)dr["ProductID"];

                    BussLogic.IndustryProduct ip = new IndustryProduct();
                    ip.IndustryID = IndustryID;
                    ip.ProductID = ProductID;

                   if(!ip.Exists(IndustryID, ProductID))
                        ip.Add();
                }
            }

            BindData();
        }

        protected void button2_Click(object sender, EventArgs e)
        {
            string itemcode = this.txtProduct.Text.Trim();

            ProductManager pm = new ProductManager();
            DataEntity.Product  p  =  pm.GetModel(itemcode);

            if (p == null)
                return;

            BussLogic.IndustryProduct ip = new IndustryProduct();
            ip.IndustryID = IndustryID;
            ip.ProductID = p.ProductID;

            if (!ip.Exists(IndustryID, p.ProductID))
                ip.Add();


            BindData();
        }

        protected void grdProductList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int productid = (int)this.grdProductList.DataKeys[e.RowIndex].Value;

            (new BussLogic.IndustryProduct()).Delete(IndustryID, productid);

            BindData();
        }
    }
}
